﻿Imports System.Data.SqlServerCe

Public Class frmReindex

    Private _Server As ReportServer
    Private _DatabasePath As String = System.IO.Path.Combine(Application.StartupPath, "RSToolKitDB.sdf")
    Private _ConnectionString As String = "Data Source=" + _DatabasePath + ";Persist Security Info=False;"
    Private Connection As SqlCeConnection
    Private ReIndex As ReindexServer

    Public Property Server() As ReportServer
        Get
            Return _Server
        End Get
        Set(ByVal value As ReportServer)
            _Server = value
        End Set
    End Property

    Private Sub frmReindex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connection = New SqlCeConnection(_ConnectionString)
        ReIndex = New ReindexServer(_Server)
        BackgroundWorker.RunWorkerAsync()
    End Sub


    Private Sub LoadData()
        Try
            Dim DataTable As New DataTable("Fields")
            Dim CommandText As String = "SELECT	ReportServer.URL, Reports.ReportGUID, Reports.ReportName, Fields.DataSet, Fields.DataSource" + _
                                        " , Fields.[Schema], Fields.ColumnOwner, Fields.ColumnName, Fields.TypeName" + _
                                        " FROM ReportServer" + _
                                        " INNER JOIN Reports ON ReportServer.ServerID = Reports.ServerID" + _
                                        " INNER JOIN Fields ON Reports.ReportID = Fields.ReportID"
            Dim Command As New SqlCeCommand(CommandText, Connection)
            Dim da As New SqlCeDataAdapter(Command)
            da.Fill(DataTable)
            dgvIndex.DataSource = DataTable
            Connection.Close()
        Catch ex As SqlCeException
            ShowException("SqlCeException", ex)
        Catch ex As Exception
            ShowException("Exception", ex)
        End Try
    End Sub

    Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
        ReIndex.Start()
    End Sub

    Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker.RunWorkerCompleted
        LoadData()
    End Sub

    Private Sub ShowException(ByVal Text As String, Optional ByVal ExceptionText As Exception = Nothing)
        Dim sqlMsgBox As New Microsoft.SqlServer.MessageBox.ExceptionMessageBox()
        sqlMsgBox.Text = Text
        sqlMsgBox.Caption = "RSToolKit Beta"
        sqlMsgBox.ShowToolBar = True
        sqlMsgBox.Buttons = Microsoft.SqlServer.MessageBox.ExceptionMessageBoxButtons.OK
        sqlMsgBox.Symbol = Microsoft.SqlServer.MessageBox.ExceptionMessageBoxSymbol.Error
        sqlMsgBox.HelpLink = "http://rstoolkit.codeplex.com/documentation"
        If Not IsNothing(ExceptionText) Then
            sqlMsgBox.InnerException = ExceptionText
        End If
        sqlMsgBox.Beep = True
        sqlMsgBox.Show(Me)
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

End Class